clear all;close all;clc
% Subject ID: 63, 64, 68, 69, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80

Subjects = {'63'};

fingers = 'abcde';
finger_names = {'Thumb';'Index';'Middle';'Ring';'Pinky'};
f_low = 0.1;
f_high = 30;
FS = 198.5;
[b,a]=butter(3,[f_low f_high]./(FS./2));

for isubject = 1:length(Subjects)
    Sub = Subjects{isubject}
    file_save = ['Save_EMG_NN_result\' Sub '_CM.mat'];
    load(file_save);
    % ==================== Plot CM =============================
    f1 = figure('position',[100 100 700 350]);
    subplot(121)
    f_plotCM(CM,100);
    colormap(flipud(gray(256)));
    colorbar;
    title('A. EMG Confusion Matrix');
    xlabel('True Finger');
    ylabel('Classified Finger');
    h = colorbar;
    h.Label.String = "Accuracy (%)";
    h.Label.Rotation = 270;
    h.Label.VerticalAlignment = "bottom";

    subplot(122)
    a_finger_test = diag(CM)./sum(CM)'*100;
    bar(a_finger_test,'facecolor',[0.5 0.5 0.5]);
    for ifinger=1:5
        pci(ifinger) = f_confid_I(20*20,a_finger_test(ifinger)/100,0.05);
    end
    hold on;
    errorbar(1:5,a_finger_test,pci*100,'.k');
    set(gca,'XTickLabel',finger_names);
    title('B. EMG Performance');
    ylabel('Accuracy (%)');
    ylim([0 100]);
    xlabel('True Finger');
    hold on;
    plot([0 6],[20 20],'k');
    axis square
    box off
    hold off;
    clear CM_test PC_subject;
end
exportgraphics(f1,'Figure5_CM.png','Resolution',300);

for i=[1 2 3 4]
    p1=a_finger_test(i)./100;
    p2=a_finger_test(5)./100;
    N1=400;N2=400;
    out = f_ztest(p1,p2,N1,N2)
end
